package cn.cena.pet.controller;

import cn.cena.basic.Exception.MyException;
import cn.cena.basic.util.AjaxResult;
import cn.cena.basic.util.LoginContextUtil;
import cn.cena.basic.util.PageList;
import cn.cena.pet.domain.Pet;
import cn.cena.pet.domain.SearchMasterMsg;
import cn.cena.pet.query.SearchMasterMsgQuery;
import cn.cena.pet.service.ISearchMasterMsgService;
import cn.cena.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {//searchMasterMsg/3
    @Autowired
    private ISearchMasterMsgService seachMasterMsgService;


    /**
     * 发布寻主
     * @param smm
     * @return
     */
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg smm, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.publish(smm,logininfo.getId());
            return AjaxResult.result();
        } catch (MyException e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统异常");
        }
    }

    /**
     * 待处理宠物
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/pending")
    public PageList<SearchMasterMsg> pending(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);

            return seachMasterMsgService.pending(logininfo.getId(),query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    /**
     * 已处理宠物
     * @param query
     * @param request
     * @return
     */
    @PostMapping("/processed")
    public PageList<SearchMasterMsg> processed(@RequestBody SearchMasterMsgQuery query, HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);

            return seachMasterMsgService.processed(logininfo.getId(),query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<>();
        }
    }

    @PutMapping("/handle")
    public AjaxResult handle(@RequestBody Pet pet,HttpServletRequest request){
        try {
            Logininfo logininfo = LoginContextUtil.getLoginContext(request);
            seachMasterMsgService.handle(pet, logininfo.getId());
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统异常");
        }
    }

    @DeleteMapping("{id}")
    public AjaxResult delete(@PathVariable Long id){
        try {
            seachMasterMsgService.remove(id);
            return AjaxResult.result();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.result().setMsg("系统繁忙");
        }
    }
}
